<template>
  <div class="reviews-container">
    <div class="page-header">
      <h2>评价管理</h2>
    </div>

    <!-- 搜索和过滤 -->
    <div class="search-bar">
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="评价内容">
          <el-input v-model="searchForm.content" placeholder="请输入评价内容" clearable />
        </el-form-item>
        <el-form-item label="评价类型">
          <el-select v-model="searchForm.type" placeholder="请选择评价类型" clearable>
            <el-option label="商品评价" value="product" />
            <el-option label="文章评价" value="article" />
            <el-option label="视频评价" value="video" />
          </el-select>
        </el-form-item>
        <el-form-item label="评分">
          <el-select v-model="searchForm.rating" placeholder="请选择评分" clearable>
            <el-option label="5星" value="5" />
            <el-option label="4星" value="4" />
            <el-option label="3星" value="3" />
            <el-option label="2星" value="2" />
            <el-option label="1星" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option label="待审核" value="pending" />
            <el-option label="已通过" value="approved" />
            <el-option label="已拒绝" value="rejected" />
          </el-select>
        </el-form-item>
        <el-form-item label="评价时间">
          <el-date-picker
            v-model="searchForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 评价列表 -->
    <el-table :data="reviewList" border style="width: 100%">
      <el-table-column prop="review_id" label="ID" width="80" />
      <el-table-column label="评价对象" min-width="200">
        <template #default="{ row }">
          <div class="review-target">
            <el-image 
              :src="row.target_image" 
              fit="cover"
              style="width: 40px; height: 40px"
            >
              <template #error>
                <div class="image-placeholder">暂无图片</div>
              </template>
            </el-image>
            <div class="target-info">
              <div class="target-name">{{ row.target_name }}</div>
              <div class="target-type">{{ getTypeText(row.type) }}</div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="评分" width="120">
        <template #default="{ row }">
          <el-rate v-model="row.rating" disabled />
        </template>
      </el-table-column>
      <el-table-column prop="content" label="评价内容" min-width="300" show-overflow-tooltip />
      <el-table-column label="评价图片" width="150">
        <template #default="{ row }">
          <div class="review-images">
            <el-image
              v-for="(image, index) in row.images"
              :key="index"
              :src="image"
              :preview-src-list="row.images"
              fit="cover"
              style="width: 40px; height: 40px; margin-right: 5px"
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="user_name" label="评价用户" width="120" />
      <el-table-column prop="create_time" label="评价时间" width="180" />
      <el-table-column label="状态" width="100">
        <template #default="{ row }">
          <el-tag :type="getStatusType(row.status)">{{ getStatusText(row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="{ row }">
          <el-button size="small" @click="handleView(row)">查看</el-button>
          <el-button 
            size="small" 
            type="success" 
            @click="handleApprove(row)"
            v-if="row.status === 'pending'"
          >通过</el-button>
          <el-button 
            size="small" 
            type="danger" 
            @click="handleReject(row)"
            v-if="row.status === 'pending'"
          >拒绝</el-button>
          <el-button 
            size="small" 
            type="primary" 
            @click="handleReply(row)"
            v-if="row.status === 'approved' && !row.has_reply"
          >回复</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 查看评价详情对话框 -->
    <el-dialog v-model="viewDialogVisible" title="评价详情" width="800px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="评价ID">{{ currentReview.review_id }}</el-descriptions-item>
        <el-descriptions-item label="评价类型">{{ getTypeText(currentReview.type) }}</el-descriptions-item>
        <el-descriptions-item label="评价对象">{{ currentReview.target_name }}</el-descriptions-item>
        <el-descriptions-item label="评分">
          <el-rate v-model="currentReview.rating" disabled />
        </el-descriptions-item>
        <el-descriptions-item label="评价用户">{{ currentReview.user_name }}</el-descriptions-item>
        <el-descriptions-item label="评价时间">{{ currentReview.create_time }}</el-descriptions-item>
        <el-descriptions-item label="状态">{{ getStatusText(currentReview.status) }}</el-descriptions-item>
        <el-descriptions-item label="审核时间" v-if="currentReview.audit_time">
          {{ currentReview.audit_time }}
        </el-descriptions-item>
        <el-descriptions-item label="评价内容" :span="2">{{ currentReview.content }}</el-descriptions-item>
      </el-descriptions>

      <div class="section-title">评价图片</div>
      <div class="review-images-preview">
        <el-image
          v-for="(image, index) in currentReview.images"
          :key="index"
          :src="image"
          :preview-src-list="currentReview.images"
          fit="cover"
          style="width: 120px; height: 120px; margin: 5px"
        />
      </div>

      <template v-if="currentReview.reply">
        <div class="section-title">商家回复</div>
        <div class="reply-content">
          <div class="reply-info">
            <span class="reply-time">{{ currentReview.reply_time }}</span>
          </div>
          <div class="reply-text">{{ currentReview.reply }}</div>
        </div>
      </template>
    </el-dialog>

    <!-- 回复评价对话框 -->
    <el-dialog v-model="replyDialogVisible" title="回复评价" width="600px">
      <el-form :model="replyForm" :rules="replyRules" ref="replyFormRef" label-width="100px">
        <el-form-item label="回复内容" prop="content">
          <el-input 
            type="textarea" 
            v-model="replyForm.content" 
            rows="4"
            placeholder="请输入回复内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="replyDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSaveReply">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 搜索表单
const searchForm = ref({
  content: '',
  type: '',
  rating: '',
  status: '',
  dateRange: []
})

// 评价列表数据
const reviewList = ref([
  {
    review_id: 1,
    type: 'product',
    target_name: '人参',
    target_image: 'https://example.com/product1.jpg',
    rating: 5,
    content: '商品质量很好，包装也很精美，服用后感觉效果不错。',
    images: ['https://example.com/review1-1.jpg', 'https://example.com/review1-2.jpg'],
    user_name: '张三',
    create_time: '2024-03-15 10:00:00',
    status: 'approved',
    has_reply: true,
    reply: '感谢您的支持，我们会继续提供优质的产品。',
    reply_time: '2024-03-15 11:00:00'
  },
  {
    review_id: 2,
    type: 'article',
    target_name: '养生知识分享',
    target_image: 'https://example.com/article1.jpg',
    rating: 4,
    content: '文章内容很实用，讲解也很详细。',
    images: [],
    user_name: '李四',
    create_time: '2024-03-15 14:00:00',
    status: 'pending',
    has_reply: false
  },
  {
    review_id: 3,
    type: 'video',
    target_name: '每日瑜伽课程',
    target_image: 'https://example.com/video1.jpg',
    rating: 5,
    content: '老师讲解很专业，动作示范很清晰。',
    images: ['https://example.com/review3.jpg'],
    user_name: '王五',
    create_time: '2024-03-15 16:00:00',
    status: 'rejected',
    has_reply: false,
    audit_time: '2024-03-15 16:30:00'
  }
])

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(3)

// 对话框相关
const viewDialogVisible = ref(false)
const replyDialogVisible = ref(false)
const currentReview = ref({})
const replyFormRef = ref(null)

// 回复表单
const replyForm = ref({
  content: ''
})

// 回复表单校验规则
const replyRules = {
  content: [
    { required: true, message: '请输入回复内容', trigger: 'blur' },
    { min: 2, max: 200, message: '长度在 2 到 200 个字符', trigger: 'blur' }
  ]
}

// 获取评价类型文本
const getTypeText = (type) => {
  const texts = {
    'product': '商品评价',
    'article': '文章评价',
    'video': '视频评价'
  }
  return texts[type] || type
}

// 获取状态标签类型
const getStatusType = (status) => {
  const types = {
    'pending': 'warning',
    'approved': 'success',
    'rejected': 'danger'
  }
  return types[status] || 'info'
}

// 获取状态文本
const getStatusText = (status) => {
  const texts = {
    'pending': '待审核',
    'approved': '已通过',
    'rejected': '已拒绝'
  }
  return texts[status] || status
}

// 搜索处理
const handleSearch = () => {
  // TODO: 实现搜索逻辑
  console.log('搜索条件:', searchForm.value)
}

// 重置搜索
const resetSearch = () => {
  searchForm.value = {
    content: '',
    type: '',
    rating: '',
    status: '',
    dateRange: []
  }
  handleSearch()
}

// 查看评价
const handleView = (row) => {
  currentReview.value = { ...row }
  viewDialogVisible.value = true
}

// 通过评价
const handleApprove = (row) => {
  ElMessageBox.confirm(
    `确定要通过该评价吗？`,
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    // TODO: 实现通过逻辑
    ElMessage.success('操作成功')
  }).catch(() => {
    ElMessage.info('已取消操作')
  })
}

// 拒绝评价
const handleReject = (row) => {
  ElMessageBox.confirm(
    `确定要拒绝该评价吗？`,
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    // TODO: 实现拒绝逻辑
    ElMessage.success('操作成功')
  }).catch(() => {
    ElMessage.info('已取消操作')
  })
}

// 回复评价
const handleReply = (row) => {
  currentReview.value = { ...row }
  replyForm.value = {
    content: ''
  }
  replyDialogVisible.value = true
}

// 保存回复
const handleSaveReply = () => {
  replyFormRef.value?.validate((valid) => {
    if (valid) {
      // TODO: 实现保存回复逻辑
      ElMessage.success('回复成功')
      replyDialogVisible.value = false
    }
  })
}

// 分页大小改变
const handleSizeChange = (val) => {
  pageSize.value = val
  // TODO: 重新加载数据
}

// 当前页改变
const handleCurrentChange = (val) => {
  currentPage.value = val
  // TODO: 重新加载数据
}
</script>

<style scoped>
.reviews-container {
  padding: 20px;
}

.page-header {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-bar {
  margin-bottom: 20px;
  padding: 20px;
  background: #fff;
  border-radius: 4px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.image-placeholder {
  width: 40px;
  height: 40px;
  background: #f5f7fa;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #909399;
  font-size: 12px;
}

.review-target {
  display: flex;
  align-items: center;
  gap: 10px;
}

.target-info {
  display: flex;
  flex-direction: column;
}

.target-name {
  font-weight: bold;
}

.target-type {
  font-size: 12px;
  color: #909399;
}

.review-images {
  display: flex;
  gap: 5px;
}

.section-title {
  font-size: 16px;
  font-weight: bold;
  margin: 20px 0;
  padding-top: 20px;
  border-top: 1px solid #ebeef5;
}

.review-images-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.reply-content {
  background: #f5f7fa;
  padding: 15px;
  border-radius: 4px;
}

.reply-info {
  margin-bottom: 10px;
}

.reply-time {
  color: #909399;
  font-size: 12px;
}

.reply-text {
  color: #606266;
  line-height: 1.5;
}
</style>
</rewritten_file>